package com.quanyan.ic.http.controller;

import com.alibaba.fastjson.JSON;
import com.quanyan.ic.common.http.APIResponse;
import com.quanyan.ic.middleware.mq.rocketmq.producer.SendCallbackListener;
import com.quanyan.ic.model.LightOp;
import com.quanyan.ic.model.SerialPortExp;
import com.quanyan.ic.mq.LightOpMessageProducer;
import com.quanyan.ic.mq.SerialPortExpMessageProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 灯控系统分析，数据收集接口
 */
@RestController
@RequestMapping("/larkic/light/systemAnalysis")
public class SystemAnalysisController {

    private static final Logger logger = LoggerFactory.getLogger(SystemAnalysisController.class);

    @Autowired
    private LightOpMessageProducer lightOpMessageProducer;
    @Autowired
    private SerialPortExpMessageProducer serialPortExpMessageProducer;

    /**
     * 开灯操作上报（包括自动和手动）
     */
    @RequestMapping("/report/lightOp")
    APIResponse reportLightOp(@RequestBody LightOp lightOp) {
        logger.info("lightOp: " + JSON.toJSON(lightOp));
        lightOpMessageProducer.asyncSend(String.valueOf(lightOp.hashCode()), JSON.toJSONString(lightOp).getBytes(), new SendCallbackListener() {
            @Override
            public void onSuccess(String sendResult) {
                logger.info(sendResult);
            }
            @Override
            public void onException(Throwable e) {
                logger.warn(e.getMessage());
            }
        });
        return APIResponse.returnSuccess();
    }

    /**
     * 串口异常上报
     */
    @RequestMapping("/report/serialPortExp")
    APIResponse serialPortExp(@RequestBody SerialPortExp serialPortExp) {
        logger.info("serialPortExp: " + JSON.toJSON(serialPortExp));
        serialPortExpMessageProducer.asyncSend(String.valueOf(serialPortExp.hashCode()), JSON.toJSONString(serialPortExp).getBytes(), new SendCallbackListener() {
            @Override
            public void onSuccess(String sendResult) {
                logger.info(sendResult);
            }
            @Override
            public void onException(Throwable e) {
                logger.warn(e.getMessage());
            }
        });
        return APIResponse.returnSuccess();
    }

}
